package cn.snmp.mutual.common;

import cn.snmp.mutual.common.snmp.SnmpNioTarget;
import cn.snmp.mutual.nms.SnmpVarBind;


import cn.snmp.mutual.data.ObjService;
import lombok.extern.slf4j.Slf4j;

/**
 * @author dengkn
 * */
@Slf4j
public class SnmpSetPread {
	

	/**
	 * 解析写数据返回值
	 * @param target SnmpNioTarget实例
	 * @return 表结构和值
	 */
	public static void parser(SnmpNioTarget target) {
		try {
			SnmpVarBind[] resultBinds = (SnmpVarBind[]) target.getOrgResult();
			ObjService result = target.getResult();
			result.setValue("isFinish", "true");
			if (resultBinds != null) {
				ObjService agentReturn = new ObjService("AgentReturn");
				for (int i = 0; i < resultBinds.length; i++) {
					agentReturn.setValue(resultBinds[i].getObjectID().toString(),
							resultBinds[i].getVariable().toString());
				}
				result.addContainedObject(agentReturn);
				log.debug("the result return by snmpset");
				result.setValue("ErrCode", "0");
				result.setValue("ErrDesc", "Set Operation Success");
			} else {
				result.setValue("ErrCode", "-1");
				result.setValue("ErrDesc", "Return NULL");
			}
			resultBinds = null;
		} catch (Exception e) {
			log.error("Snmp set operator result error!" , e);
			target.getResult().setValue("ErrCode", "801");
			target.getResult().setValue("ErrDesc", e.getMessage());
		}
	}
	
	public static void parser4Error(SnmpNioTarget target) {
		try {
			SnmpVarBind[] resultBinds = (SnmpVarBind[]) target.getOrgResult();
			ObjService result = target.getResult();
			result.setValue("isFinish", "true");
			if (resultBinds != null) {
				ObjService agentReturn = new ObjService("ErrorAgentReturn");
				for (int i = 0; i < resultBinds.length; i++) {
					agentReturn.setValue(resultBinds[i].getObjectID().toString(),
							resultBinds[i].getVariable().toString());
				}
				result.addContainedObject(agentReturn);
				log.debug("the result return by snmpset");
			}
			resultBinds = null;
		} catch (Exception e) {
			log.error("Snmp set operator result error!" , e);
		}
	}
}
